package com.jlcloud.core.oauth2.exception;

import io.jsonwebtoken.JwtException;
import com.jlcloud.core.oauth2.endpoint.OAuth2AuthorizationEndpoint;
import com.jlcloud.core.oauth2.endpoint.OAuth2SocialEndpoint;
import com.jlcloud.core.oauth2.endpoint.OAuth2TokenEndPoint;
import com.jlcloud.core.oauth2.provider.OAuth2Response;
import com.jlcloud.core.secure.exception.SecureException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * OAuth2ExceptionHandler
 *
 * @author By: JlCloud <br>
 * @Package: com.jlcloud <br>
 * @CreateTime: 2024-12-03 21:45 <br>
 * @Copyright: 2021 www.jilianjituan.com Inc. All rights reserved. <br>
 * @Caution 注意：本内容仅限于冀联人力集团内部传阅，禁止外泄以及用于其他的商业目的 <br>
 */
@ControllerAdvice(basePackageClasses = {OAuth2AuthorizationEndpoint.class, OAuth2SocialEndpoint.class, OAuth2TokenEndPoint.class})
public class OAuth2ExceptionHandler {
	@ExceptionHandler(OAuth2Exception.class)
	public ResponseEntity<?> handleOAuth2Exception(OAuth2Exception ex) {
		// 统一处理验证失败的情况
		return ResponseEntity.ok(OAuth2Response.create().ofFailure(ex.getExceptionCode().getCode(), ex.getMessage()));
	}

	@ExceptionHandler(SecureException.class)
	public ResponseEntity<?> handleSecureException(SecureException ex) {
		// 统一处理验证失败的情况
		return ResponseEntity.ok(OAuth2Response.create().ofFailure(OAuth2ErrorCode.INVALID_USER, ex.getMessage()));
	}

	@ExceptionHandler(JwtException.class)
	public ResponseEntity<?> handleJwtException(JwtException ex) {
		// 统一处理验证失败的情况
		return ResponseEntity.ok(OAuth2Response.create().ofFailure(OAuth2ErrorCode.ACCESS_DENIED, ex.getMessage()));
	}
}
